/*
 * Copyright (C) 2011-2015 ShenZhen iBOXPAY Information Technology Co.,Ltd.
 * 
 * All right reserved.
 * 
 * This software is the confidential and proprietary
 * information of iBoxPay Company of China. 
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement 
 * you entered into with iBoxpay inc.
 *
 */

package com.iboxpay.sdk;

import java.io.UnsupportedEncodingException;
import java.util.Map;

//import org.apache.commons.codec.binary.Base64;

/**
 * The class RandomSignUtil.
 *
 * Description: 
 *
 * @author: nieminjie
 * @since: 2016年2月29日	
 * @version: $Revision$ $Date$ $LastChangedBy$
 *
 */

public class RandomSignUtil {

	private static final String ENCODE_UTF8 = "UTF-8";

	public static void main(String[] args) {
		Map<String, Object> keys = RSACoder.getKeys();
		String publickey = (String) keys.get(RSACoder.PUBLIC_KEY);
		String privatekey = (String) keys.get(RSACoder.PRIVATE_KEY);
		String sign = encryptWithRSA("abc", privatekey, false);
		System.out.println(sign);
		System.out.println(getDecryptReqContentWithRsa3Des(sign, publickey, false));
	}

	/**
	 * 默认公钥加密方法
	 * @param signContentJson 加密内容json字符串
	 * @param key 密钥
	 * @return
	 */
	public static String encryptWithRSA(String signContentJson, String key) {
		return encryptWithRSA(signContentJson, key, true);
	}

	/**
	 * 加密方法
	 * @param signContentJson 加密内容json字符串
	 * @param key 密钥
	 * @param isPublic 是否公钥加密
	 * @return
	 */
	public static String encryptWithRSA(String signContentJson, String key, boolean isPublic) {
		String rundomNumber = Coder.init3DesKey(null);

		// 加密3des 随机数
		String encryptDes3RandomKey;
		// DES3加密报文
		String encryptDES3Content;
		// 加密内容
		String signContent = null;
		try {

			byte[] unEncodedContent = null;

			// 使用工具加密
			if (isPublic) {
				unEncodedContent = RSACoder.encryptByPublicKey(rundomNumber.getBytes(), key);
			} else {
				unEncodedContent = RSACoder.encryptByPrivateKey(rundomNumber.getBytes(), key);
			}
			encryptDes3RandomKey ="" ;//new String(Base64.encodeBase64(unEncodedContent), ENCODE_UTF8);

			encryptDES3Content = "";//new String(Base64.encodeBase64(Des3Coder.encrypt(signContentJson.getBytes(), rundomNumber.getBytes())), ENCODE_UTF8);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		signContent = encryptDes3RandomKey + "|" + encryptDES3Content;
		return signContent;
	}

	/**
	 * 私钥解密获取加密内容
	 * @param signContentCiphertext
	 * @param privateKey 私钥
	 * @return
	 */
	public static String getDecryptReqContentWithRsa3Des(String signContentCiphertext, String privateKey) {
		return getDecryptReqContentWithRsa3Des(signContentCiphertext, privateKey, true);
	}

	/**
	 * 解密获取加密内容
	 * @param signContentCiphertext
	 * @param key 密钥
	 * @param isPrivate 是否私钥
	 * @return
	 */
	public static String getDecryptReqContentWithRsa3Des(String signContentCiphertext, String key, boolean isPrivate) {
		PubPriKey keyObj = new PubPriKey();
		if (isPrivate) {
			keyObj.setPrivateKey(key);
		} else {
			keyObj.setPublicKey(key);
		}
		return getDecryptReqContentWithRsa3Des(signContentCiphertext, keyObj, isPrivate);
	}

	/**
	 * 私钥解密获取加密内容
	 * @param signContentCiphertext
	 * @param key 公私钥对象
	 * @return
	 */
	public static String getDecryptReqContentWithRsa3Des(String signContentCiphertext, PubPriKey key) {
		return getDecryptReqContentWithRsa3Des(signContentCiphertext, key, true);
	}

	/**
	 * 获取加密内容
	 * @param signContentCiphertext
	 * @param key 公私钥对象
	 * @param isPrivate 是否私钥
	 * @return
	 */
	public static String getDecryptReqContentWithRsa3Des(String signContentCiphertext, PubPriKey key, boolean isPrivate) {
		String[] signContent = signContentCiphertext.split("\\|");
		String requestRandomDes3Key = signContent[0];
		String requestParamsContent = signContent[1];
		String randomDes3Key = "";
		String paramsContentPlaintext = "";
		try {
			//解密requestRandomDes3Key
			if (isPrivate) {
				randomDes3Key = "";//new String(RSACoder.decryptByPrivateKey(Base64.decodeBase64(requestRandomDes3Key.getBytes()), key.getPrivateKey()), ENCODE_UTF8);
			} else {
				randomDes3Key = "";//new String(RSACoder.decryptByPublicKey(Base64.decodeBase64(requestRandomDes3Key.getBytes()), key.getPublicKey()), ENCODE_UTF8);
			}
			paramsContentPlaintext = "";//new String(Des3Coder.decrypt(Base64.decodeBase64(requestParamsContent.getBytes()), randomDes3Key.getBytes()), ENCODE_UTF8);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramsContentPlaintext;
	}
}
